package game.phases.actionphase.commands 
{
	/**
	 * ...
	 * @author rnk
	 * 
	 */
	public class BattleCalculator 
	{
		private var attacker:int;
		private var defender:int;
		private var attackerTactics:int;
		private var defenderTactics:int;
		
		public function BattleCalculator(attacker:int,attackerTactics:int,defender:int,defenderTactics:int) 
		{
			this.defenderTactics = defenderTactics;
			this.attackerTactics = attackerTactics;
			this.defender = defender;
			this.attacker = attacker;
			
		}
		
		public function GetResult():Object
		{
			var result:Object = { attacker:{},defender:{}};
			
			//get random power from absolute power and tactics
			result.attacker.power = GetPower(attacker, attackerTactics);
			result.defender.power = GetPower(defender, defenderTactics);
			
			//calculate inflicted damage from power and tactics
			result.attacker.damage = GetDamage(result.attacker.power, attackerTactics);
			result.defender.damage = GetDamage(result.defender.power, defenderTactics);
			
			//calculate received damage from enemies inflicted damage and tactics
			result.attacker.casualties = GetDamage(result.defender.damage, attackerTactics);
			result.defender.casualties = GetDamage(result.attacker.damage, defenderTactics);
			
			//decide who won by power difference
			var powerDifference:int = result.attacker.power - result.defender.power;
			if (powerDifference > 0) 
			{
				result.winner = 0;//attacker wins
				
			} else
			if (powerDifference < 0) 
			{
				result.winner = 1;//defender wins
			} else
			{
				result.winner = 2;//noone wins
			}
			
			return result;
		}
		
		private function GetDamage(power:int, tactics:int):int
		{
			switch (tactics) 
			{
				case 0: //agressive
					return Math.round((power / 4) * 3);
				break;
				case 1: //careful
					return Math.round(power / 2);
				break;
				default://defensive
					return Math.round(power / 4);
				break;
			}
			return 0;
		}
		
		public function GetPower(totalPower:int,tactics:int):int
		{
			switch (tactics) 
			{
				case 0: //agressive
					// random(floor(power/2)*2 + ceil(power/2))
					return Math.round(Math.random() * (Math.floor(totalPower / 2) * 2 + Math.ceil(totalPower / 2)));
				break;
				case 1: //careful
					// floor(power/2) + random(ceil(power/2))
					return Math.floor(totalPower / 2)  + Math.round(Math.random() * Math.ceil(totalPower / 2));
				break;
				default://defensive
					// floor(power/2) + floor(ceil(power/2)/2)
					return Math.floor(totalPower / 2)  + Math.floor(Math.ceil(totalPower / 2) / 2);
				break;
			}
			return 0;
		}
		
		public static function GetTacticsPowerText(totalPower:int,tactics:int):String
		{
			switch (tactics) 
			{
				case 0: //agressive
					return "0-" + String(Math.floor(totalPower / 2) * 2 + Math.ceil(totalPower / 2));
				break;
				case 1: //careful
					return String(Math.floor(totalPower / 2)) + "-" + String(Math.floor(totalPower / 2) + Math.ceil(totalPower / 2));
				break;
				default://defensive
					return String(Math.floor(totalPower / 2)  + Math.floor(Math.ceil(totalPower / 2) / 2));
				break;
			}
			return 0;
			
		}
		
	}

}